﻿package kliment.display.draw.shape {
	import flash.geom.Point;
	import kliment.display.draw.DrawStyle;
	import kliment.display.draw.style.LineStyle;
	
	/**
	 * ...
	 * @author Kliment
	 */
	public class SpiralShape extends ShapeAbstract {
		private var STEP_SIZE:int = 5;
		
		public function SpiralShape(center:Point, xRadius:Number = 0, yRadius:Number = 0, xGrowth:Number = 9, yGrowth:Number = 9, startAngle:Number = 0, revolutions:Number = 9, drawStyle:DrawStyle = null) {
			super();
			if (drawStyle.lineStyle == null)
				drawStyle.lineStyle = new LineStyle(1);
			
			_setStyles(graphics, drawStyle);
			if (yRadius == 0) {
				yRadius = xRadius;
				yGrowth = xGrowth;
			}
			else if (yGrowth == 0) {
				yGrowth = xGrowth;
			}
			if (revolutions < 0) {
				STEP_SIZE  = -STEP_SIZE;
				startAngle = -startAngle;
			}
			var startRadians:Number = -(startAngle / 180) * Math.PI;
			var endRadians:Number = -((revolutions * 360) / 180) * Math.PI;
			var theta:Number = -(STEP_SIZE / 180) * Math.PI;
			var stepsPerRevolution:Number = Math.abs((2 * Math.PI) / theta);
			var xRadiusDelta:Number = xGrowth / stepsPerRevolution;
			var yRadiusDelta:Number = yGrowth / stepsPerRevolution;
			var ax:Number = center.x + xRadius * Math.cos( startRadians );
			var ay:Number = center.y + yRadius * Math.sin( startRadians );
			var currentRadians:Number = startRadians + theta;
			
			var bx:Number, by:Number;	
			graphics.moveTo(center.x, center.y);
			graphics.moveTo(ax, ay);
			while(Math.abs(currentRadians) < Math.abs(endRadians)){
				bx = center.x + xRadius * Math.cos( currentRadians );
				by = center.y + yRadius * Math.sin( currentRadians );
				graphics.lineTo( bx, by );
				currentRadians += theta;
				xRadius += xRadiusDelta;
				yRadius += yRadiusDelta;
			}
		}
		
	}
	
}